package com.sicnustray.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sicnustray.common.Constants;
import com.sicnustray.common.Result;
import com.sicnustray.utils.annotation.MyLog;
import com.sicnustray.entity.Role;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.sicnustray.service.IRoleService;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yl
 * @since 2022-11-09
 */
@RestController
@RequestMapping("/role")
public class RoleController {


        @Autowired
        private IRoleService roleService;


        // 新增或者更新
        @RequiresPermissions("角色管理")
        @MyLog(value = "新增或者更新角色")
        @PostMapping
        public Result save(@RequestBody Role role) {

                Integer roleId=role.getId();

                if(roleId==null) {
                        String roleName = role.getName();
                        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
                        queryWrapper.eq("name",roleName);
                        Role one = roleService.getOne(queryWrapper);
                        if(one!=null){
                                return Result.error(Constants.CODE_600,"角色已存在");
                        }else {
                                return Result.success(roleService.save(role));
                        }
                }else{
                        Role oneRole=roleService.getById(roleId);
                        if(oneRole.getName().equals(role.getName())){
                                return Result.success(roleService.updateById(role));
                        }else{
                                String roleName = role.getName();
                                QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
                                queryWrapper.eq("name", roleName);
                                Role one = roleService.getOne(queryWrapper);
                                if(one!=null){
                                        return Result.error(Constants.CODE_600,"角色已存在");
                                }else{
                                        return Result.success(roleService.updateById(role));
                                }
                        }

                }

        }

        @RequiresPermissions("角色管理")
        @MyLog(value = "单个删除角色")
        @DeleteMapping("/{id}")
        public Result delete(@PathVariable Integer id) {
                roleService.deleteMenuById(id);
                return Result.success(roleService.removeById(id));
                }

        @MyLog(value = "查询角色信息")
        @GetMapping("/flag/{rolename}")
        public Result role(@PathVariable String rolename){
                QueryWrapper<Role> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("name",rolename);
                return Result.success(roleService.getOne(queryWrapper));
        }
        @RequiresPermissions("角色管理")
        @MyLog(value = "批量删除角色")
        @PostMapping("/del/batch")
        public Result deleteBatch(@RequestBody List<Integer> ids) {
                return Result.success(roleService.removeByIds(ids));
                }

        @MyLog(value = "查询所有角色")
        @GetMapping
        public Result findAll() {
                return Result.success(roleService.list());
                }

        @MyLog(value = "查询单个角色")
        @GetMapping("/{id}")
        public Result findOne(@PathVariable Integer id) {
                return Result.success(roleService.getById(id));
                }

        @MyLog(value = "分页查询角色")
        @GetMapping("/page")
        public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                               @RequestParam String name            ) {
                QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
                queryWrapper.like("name",name);
                //queryWrapper.orderByDesc("id");
                return Result.success(roleService.page(new Page<>(pageNum, pageSize), queryWrapper));
                }
        @RequiresPermissions("角色管理")
        @MyLog(value = "设置角色权限")
        @PostMapping("/roleMenu/{roleId}")
        public Result roleMenu(@PathVariable Integer roleId,@RequestBody List<Integer> menuIds) {
                roleService.setRoleMenu(roleId,menuIds);
                return Result.success();
        }

        @MyLog(value = "获取角色权限")
        @GetMapping("/roleMenu/{roleId}")
        public Result getRoleMenu(@PathVariable Integer roleId) {
                return Result.success(roleService.getRoleMenu(roleId));
        }


}

